{extend name="../../admin/view/public/admin"}

{block name='content'}

<div class="page-header">当前位置：<span class="text-primary">系统升级</span></div>

<style>

</style>

<div class="page-content">

    <form action="" method="post" class="form-validate form-horizontal ">
        <div class="panel panel-default">
            <div class="panel-heading h4">当前系统版本 v{$version}</div>
            <div class="panel-body">

                {if !empty($upgradeList)}
                <input type="hidden" name="version" value="{$upgradeList[0]['version']}">
                {/if}

                {if !empty($updateFiles)}

                    <div class="form-group">
                        <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">文件同步情况</label>
                        <div class="col-sm-10">
                            <div class="help-block"><strong>注意: 重要: 本次更新涉及到程序变动, 请做好备份.</strong></div>
                            <div class="alert alert-info" style="line-height:20px;margin-top:20px;color:#2d8cf0;background-color: #ffffff;border-color: #2d8cf0;">
                                {foreach $updateFiles as $key => $file}
                                    <div><span style="display:inline-block; width:30px;">M</span>{$file}</div>
                                {/foreach}
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">更新协议</label>
                        <div class="col-sm-10">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" id="agreement_0"> 我已经做好了相关文件的备份工作
                                </label>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" id="agreement_1"> 认同官方的更新行为并自愿承担更新所存在的风险
                                </label>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" id="agreement_2"> 理解官方的辛勤劳动并报以感恩的心态点击更新按钮
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-md-offset-2 col-lg-offset-1 col-xs-12 col-sm-10 col-md-10 col-lg-11">
                            <button type="submit" name="update_type" value="0" class="btn btn-danger span3" style="margin-left: 20px;" onclick="return check_update();">立即更新</button>
                            <!-- <button type="submit" name="update_type" value="1" class="btn btn-info span3" style="margin-left: 20px;" onclick="return check_update();">2.zip下载更新</button> -->
                            <button type="submit" name="is_update" value="1" class="btn btn-primary span3" style="margin-left: 20px;">立即监测更新</button>
                            <!--<a class="btn btn-default span3" href="https://www.xsyq.cn/xsframe.zip" download="星数引擎.zip">下载v{if !empty($upgradeList)}{$upgradeList[0]['version']}{/if}补丁包</a>-->
                        </div>
                    </div>
                {else}
                    <div class="alert alert-danger">
                        <i class="fa fa-exclamation-triangle"></i> 更新时请注意备份网站数据和相关数据库文件！官方不强制要求用户跟随官方意愿进行更新尝试！
                    </div>

                    <div class="alert alert-info">
                        <i class="fa fa-check"></i> 当前系统未检测到有新版本,已是最新版本
                    </div>

                    <div class="form-group">
                        <div class="col-sm-9">
                            <button type="submit" name="is_update" value="1" class="btn btn-primary span3" style="margin-left: 5px;">立即监测更新</button>
                        </div>
                    </div>

                {/if}
            </div>
        </div>
    </form>

    <div class="row">
        <div class="col-lg-12">
            <div class="alert alert-info refresh-log" style="color:#2d8cf0;background-color: #ffffff;border-color: #2d8cf0;">
                <h2><i class="fa fa-refresh"></i> 更新日志</h2>
                <ul class="list-unstyled" style="margin-top: 15px;">
                    {foreach $upgradeList as $key => $row}
                    <li style="line-height: 28px;font-size: 14px;"><span class="pull-right"> {:date('Y-m-d H:i:s',$row['updatetime'])}</span><a href="javascript:;" title="{$row['title']}" onclick="ajaxModel(this)" data-url="{:webUrl('sysset/versionContent',['version' => $row['version'],'content' => $row['content']])}">{$row['version']} {$row['title']}</a></li>
                    {/foreach}
                </ul>
            </div>
        </div>
    </div>

</div>

{/block}

{block name='script'}
    <script type="text/javascript">
        function check_update(){
            if(!$("#agreement_0").is(':checked')){
                tip.msgbox.err("请选择>我已经做好了相关文件的备份工作！")
                return false;
            }
            if(!$("#agreement_1").is(':checked')){
                tip.msgbox.err("请选择>认同官方的更新行为并自愿承担更新所存在的风险！")
                return false;
            }
            if(!$("#agreement_2").is(':checked')){
                tip.msgbox.err("请选择>理解官方的辛勤劳动并报以感恩的心态点击更新按钮！")
                return false;
            }
        }
        // 监测更新

        // 监测更新
        let isOwner = parseInt("{php}echo in_array($userInfo['role'],['owner']) ? 1 : 0;{/php}");
        let isSystem = parseInt("{$isSystem?1:0}");

        if( isOwner === 1 && isSystem === 1 ){
            let isNewUpgrade = parseInt("{:count($updateFiles ?? [])}");
            if( isNewUpgrade <= 0 ){
                checkSystemVersion();
            }
            async function checkSystemVersion() {
                try {
                    const response = await fetch("{:webUrl('sysset/checkVersion')}");
                    const data = await response.json();
                    let isUpgrade = data.result.isUpgrade;
                    if( isUpgrade ){
                        $("button[name=is_update]").trigger('click')
                    }
                } catch (error) {
                    console.error('Error fetching data:', error);
                }
            }
        }
    </script>
{/block}